package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    //查询所有角色信息
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page=roleDao.findPage(queryString);
        long total = page.getTotal();
        List<Role> result = page.getResult();
        return new PageResult(total,result);
    }
    //添加角色信息
    @Override
    public void add(Role role, String[] permissionIds, String[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        for (String permissionId : permissionIds) {
             int Id=Integer.parseInt(permissionId.substring(1));
            Map<String,Integer> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("Id",Id);
            roleDao.setPermissionIdAndRole(map);
        }
        for (String menuId : menuIds) {
            int mId = Integer.parseInt(menuId.substring(1));
            Map<String,Integer> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("mId",mId);
            roleDao.setMenuAndRole(map);
        }
    }
    //根据id查询角色信息
    @Override
    public Role findById(Integer roleId) {

        return roleDao.findById(roleId);
    }
    //根据角色ID查询对应所有权限ID
    @Override
    public List<Permission> findpermissionIdsByroleId(Integer roleId) {

        return roleDao.findpermissionIdsByroleId(roleId);
    }
    //根据角色ID查询对应所有菜单ID
    @Override
    public List<Menu> findmenuIdsByroleId(Integer roleId) {

        return roleDao.findmenuIdsByroleId(roleId);
    }
    //编辑角色
    @Override
    public void edit(Role role, String[] permissionIds, String[] menuIds) {
        roleDao.edit(role);
        roleDao.deleteAssociationByRoleAndPermission(role.getId());
        roleDao.deleteAssociationByRoleAndMenu(role.getId());
        Integer roleId = role.getId();
        for (String permissionId : permissionIds) {
            int Id=Integer.parseInt(permissionId);
            Map<String,Integer> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("Id",Id);
            roleDao.setPermissionIdAndRole(map);
        }
        for (String menuId : menuIds) {
            int mId = Integer.parseInt(menuId);
            Map<String,Integer> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("mId",mId);
            roleDao.setMenuAndRole(map);
        }

    }

    @Override
    public void delete(Integer id) {
        List<Permission> permissionList = findpermissionIdsByroleId(id);
        List<Menu> menuList = findmenuIdsByroleId(id);
        if ((menuList!=null&&menuList.size()>0)||(permissionList!=null&&permissionList.size()>0)){
            throw  new RuntimeException("有关联关系，删除失败");
        }else {
            roleDao.delete(id);
        }

    }
}
